package com.ad.admin.andriodthread;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.SeekBar;

public class MainActivity extends AppCompatActivity {

    Intent intent;
    private SeekBar seekBar;
    private MusicService.MyBinder mm;
    private int process = 0;
    private Handler handler;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        intent = new Intent("musicser");
        seekBar=(SeekBar)findViewById(R.id.sb);
        handler = new Handler(){
            @Override
            public void handleMessage(Message msg) {
                int pro = msg.getData().getInt("pro");
                seekBar.setProgress(pro);
            }
        };


        bindService(intent, new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                mm = (MusicService.MyBinder)service;
                Thread t = new Thread(){
                    @Override
                    public void run() {
                        Log.i("gtq","activity........"+Thread.currentThread().getName());
                        while(process < 800){
                            try {
                                Thread.sleep(30);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                            process = mm.getProcess();
                            Message msg = new Message();
                            Bundle bundle = new Bundle();
                            bundle.putInt("pro", process);
                            msg.setData(bundle);

                            handler.sendMessage(msg);
                        }
                    }
                };
                t.setDaemon(true);
                t.start();

            }

            @Override
            public void onServiceDisconnected(ComponentName name) {

            }
        },BIND_AUTO_CREATE);

    }


    public void play(View view) {
        int action = 1;
        intent.putExtra("action",action);
        startService(intent);
        ///branch
    }

    public void pause(View view) {
        int action = 2;
        intent.putExtra("action",action);
        startService(intent);

    }

    public void resume(View view) {
        int action = 3;
        intent.putExtra("action",action);
        startService(intent);
    }
}
